<?php
namespace Products\Model;

use Zend\Db\ResultSet\ResultSet;
use Zend\Db\TableGateway\TableGateway;
use Zend\Db\Sql;
use Zend\Db\Sql\Where;
use Zend\Db\Sql\Select;
use Zend\Paginator\Adapter\DbSelect;
use Zend\Paginator\Paginator;
use Zend\Db\Sql\Predicate;
use Zend\Db\Sql\Expression;

class IndustryTable
{
    protected $tableGateway;
	protected $select;
    public function __construct(TableGateway $tableGateway)
    {
        $this->tableGateway = $tableGateway;
		$this->select = new Select();
    }
	public function addindustry($adddata,$category)
    {
		if(isset($adddata['hid_industry_id']) && $adddata['hid_industry_id']!=""){
			$this->tableGateway->delete(array('(industry_id IN ('. $adddata['hid_industry_id'].'))'));	
		}
		$data = array(
				'industryname'        => ucfirst($adddata['ind_name']),
				'category_id'         => $category,
				'in_status' 	      => 1,	
			   );
		$this->tableGateway->insert($data);
		return $this->tableGateway->lastInsertValue;
    }
	public function getindustrydetails($industry_id)
    {
		$select = $this->tableGateway->getSql()->select();
		$select->join('country_and_industry', 'country_and_industry.industry_id=industry.industry_id',array('*'),'left');
		$select->where('industry.industry_id="'.$industry_id.'"');
		$resultSet = $this->tableGateway->selectWith($select);
		return $resultSet;	 
	}
	public function getallindustries()
    {
		$select = $this->tableGateway->getSql()->select();
		$resultSet = $this->tableGateway->selectWith($select);
		return $resultSet;	 
	}
	public function getindustries($industry_id)
    {
		$select = $this->tableGateway->getSql()->select();
		$select->where('industry_id="'.$industry_id.'"');
		$resultSet = $this->tableGateway->selectWith($select);
		return $resultSet;	 
	}
	public function deleteIndustry($industry_id){
		$deleteid=$this->tableGateway->delete(array('(industry_id IN ('.$industry_id.'))'));
		return $deleteid;
	}	
	public function getindustrieslist($country_id)
    {
		$select = $this->tableGateway->getSql()->select();
		$select->where('country_id="'.$country_id.'"');
		$resultSet = $this->tableGateway->selectWith($select);
		return $resultSet;	 
	}
	public function getindustriesBasedCountry($industry_id,$country_id){
		$select = $this->tableGateway->getSql()->select();
		$select->where('country_id="'.$country_id.'"');
		$select->where('industry_id="'.$industry_id.'"');
		$resultSet = $this->tableGateway->selectWith($select);
		return $resultSet->current();
	}
	public function getindustriesCategories($industry_id){
		$select = $this->tableGateway->getSql()->select();
		$select->where('industry_id="'.$industry_id.'"');
		$resultSet = $this->tableGateway->selectWith($select);
		return $resultSet->current();
	}
	public function checkIndustryName($industryName,$country_id){
		$ind_name=ucfirst($industryName);
		$select = $this->tableGateway->getSql()->select();
		$select->where('industryname="'.$ind_name.'"');
		$select->where('country_id="'.$country_id.'"');
		$resultSet = $this->tableGateway->selectWith($select);
		return $resultSet->count();
	}
	public function getindustry($industry_id)
    {
		$select = $this->tableGateway->getSql()->select();
		$select->join('country_and_industry', 'country_and_industry.industry_id=industry.industry_id',array('*'),'left');
		$select->where('industry.industry_id="'.$industry_id.'"');
		$resultSet = $this->tableGateway->selectWith($select);
		return $resultSet->current();	 
	}
}